package com.example.myapplication.subsistencepayment;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.Config.Commom;
import com.example.myapplication.Config.OKHttpTool;
import com.example.myapplication.R;
import com.example.myapplication.ShuJu.LeavecommentBean;

import org.json.JSONException;
import org.json.JSONObject;

public class DetailspageActivity extends AppCompatActivity {

    private ImageView img;
    private RadioGroup radio;
    private TextView submit;
    private String IPHOME;
    private TextView iphome;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detailspage);
        initview();
        //返回
        but_bloak();
        //点击充值
        recharge();
    }

    private void recharge() {
        int checkedRadioButtonId = radio.getCheckedRadioButtonId();
        String rechargeAmount = "";
        if(checkedRadioButtonId==R.id.ra1){
            rechargeAmount = "50" ;
        }else if(checkedRadioButtonId==R.id.ra2){
            rechargeAmount = "100" ;
        }else if(checkedRadioButtonId==R.id.ra3){
            rechargeAmount = "200" ;
        }

        try {
            JSONObject jsonObject = new JSONObject();
            //电子支付 微信 支付宝
            jsonObject.put("paymentType","微信");
            jsonObject.put("phonenumber",IPHOME);
            jsonObject.put("rechargeAmount",rechargeAmount);
            jsonObject.put("ruleId","1");
            jsonObject.put("type","2");
            String JSON = jsonObject.toString();
            sendrecharge(JSON);

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void sendrecharge(String JSON) {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OKHttpTool.doNetRequest("prod-api/api/living/phone/recharge",
                        OKHttpTool.POST,
                        LeavecommentBean.class,
                        new OKHttpTool.NetCallback() {
                            @Override
                            public void onSuccess(Object obj) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        LeavecommentBean leavecommentBean = (LeavecommentBean)obj;
                                        if(leavecommentBean.getCode()==200){
                                            Toast.makeText(DetailspageActivity.this,"充值成功",Toast.LENGTH_SHORT).show();
                                            DetailspageActivity.this.finish();
                                        }
                                    }
                                });
                            }

                            @Override
                            public void onFailure(String msg) {

                            }
                        },JSON);
            }
        });
    }

    private void but_bloak() {
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DetailspageActivity.this.finish();
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        IPHOME = intent.getStringExtra(Commom.IPHOME);

        img = findViewById(R.id.img);
        submit = findViewById(R.id.submit);
        radio = findViewById(R.id.radio);
        iphome = findViewById(R.id.iphome);
        iphome.setText(this.IPHOME);

    }
}